{include file="public/header" /}
<style>
    #table th, #table td {
        text-align: center;
    }
    .x-sort {
        height: 24px !important;
    }
    .layui-table thead {
        background: rgb(231, 238, 253, 0.4);
        color: #515a6e;
    }
    .layui-table thead th {
        height: 28px;
        line-height: 28px;
    }
    .layui-table tbody td {
        height: 35px;
        line-height: 35px;
    }
    .layui-table .layui-form-switch {
        margin-top: initial;
    }
    .layui-btn + .layui-btn {
        margin-left: 0;
    }
</style>
<body>
<div class="layui-fluid layui-anim layui-leftbit">
    <div class="layui-card" style="padding-top: 5px">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius" id="create">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>备份
                </a>
                <a class="layui-btn layui-btn-danger layui-btn-sm layui-btn-radius clear">
                    <i class="layui-icon layui-icon-delete"></i>清空数据
                </a>
                <a class="layui-btn layui-btn-danger layui-btn-sm layui-btn-radius deletes">
                    <i class="layui-icon layui-icon-delete"></i>批量删除
                </a>
            </div>
            <table class="layui-table layui-form" id="table" lay-size="sm">
                <thead>
                    <tr>
                        <th style="width: 15px"><input type="checkbox" lay-filter="list_all" lay-skin="primary"></th>
                        <th>文件名</th>
                        <th style="width: 150px">文件大小</th>
                        <th style="width: 150px">备份时间</th>
                        <th style="width: 100px">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {volist name="list" id="v"}
                    <tr class="data_line" data_id="{$v.file}">
                        <td>
                            {if $v.is_del}
                            <input type="checkbox" lay-filter="list_checkbox" lay-skin="primary" value="{$v.file}">
                            {/if}
                        </td>
                        <td>{$v.file}</td>
                        <td>{$v.size}</td>
                        <td>{:date('Y-m-d H:i:s',$v.create_time)}</td>
                        <td class="td-manage">
                            <a class="layui-btn layui-btn-warm layui-btn-sm backup" data-file="{$v.file}"
                               href="javascript:;">还原</a>
                        </td>
                    </tr>
                    {/volist}
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
{include file="public/footer" /}
<script>
    layui.use(['tree', 'form', 'jquery', 'methods', 'notify'], function () {
        var $ = layui.jquery, notify = layui.notify, f = layui.form;
        notify.init();
        f.on('checkbox(list_all)', function (obj) {
            var checked = obj.elem.checked;
            $("[lay-filter=list_checkbox]").prop("checked", checked);
            f.render('checkbox');
        });
        function layerLoad(text = '加载中....') {
            layui.layer.msg('<div><i class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i> ' + text + '', {
                shade: 0.8,
                time: 99999
            });
        }
        $('#create').click(function () {
            layerLoad()
            $.get("{:URL('backup/add')}", function (data) {
                layer.closeAll();
                if (data.code === 1) {
                    notify.success("数据备份成功", 1500, function () {
                        window.location.reload();
                    });
                } else {
                    notify.error("数据备份失败");
                }
            }, 'json');
        });
        $('.backup').click(function () {
            var file = $(this).attr('data-file');
            layer.confirm('确认要还原吗？', function () {
                layerLoad()
                $.get("{:url('backup/import')}", {file: file}, function (data) {
                    layer.closeAll();
                    if (data.code === 1) {
                        notify.success(data.msg, 1500, function () {
                            window.location.reload();//页面刷新
                        });
                    } else {
                        notify.error(data.msg);
                    }
                }, 'json');
            });
        });
        $('.clear').click(function () {
            layer.confirm('确认要清空数据吗？', function () {
                layerLoad()
                $.get("{:url('backup/clear')}", function (data) {
                    layer.closeAll();
                    if (data.code === 1) {
                        notify.success(data.msg, 1500, function () {
                            window.location.reload();//页面刷新
                        });
                    } else {
                        notify.error(data.msg);
                    }
                }, 'json');
            });
        });
        $('.delete').click(function () {
            var file = [$(this).attr('data-file')];
            del(file);
        });
        $('.deletes').click(function () {
            var arr = [];
            $("[lay-filter=list_checkbox]:checked").each(function (i, e) {
                arr.push($(this).val());
            });
            if (arr.length < 1) {
                notify.warning('请选择要删除的文件');
                return;
            }
            del(arr);
        });

        function del(file) {
            layui.layer.confirm('确认要删除吗？', function () {
                layerLoad()
                layui.jquery.get("{:url('backup/delete')}", {file: file}, function (data) {
                    layer.closeAll();
                    if (data.code === 1) {
                        notify.success(data.msg, 1500, function () {
                            window.location.reload();//页面刷新
                        });
                    } else {
                        notify.error(data.msg);
                    }
                })
            });
        }
    });
</script>
</html>